export function generateArtists(artists) {
    if (!artists || artists.length === 0) return '';
    return artists.reduce((prev, cur) => {
        if (!prev.name) return '';
        if (prev.name === cur.name) return prev.name;
        return prev.name + '/' + cur.name
    }, artists[0]);
}
// 格式化时间（00：00）
export function formatSongTime(time) {
    // 分秒
    let m = parseInt(time / 60), s = parseInt(time % 60);
    // 补零
    m = m > 9 ? m : "0" + m;
    s = s > 9 ? s : "0" + s;
    return m + ":" + s;
}
export function formatSongTimeText(timeText) {
    let arr = timeText.split(':'), m = parseInt(arr[0]) * 60, s = parseInt(arr[1]);
    return m + s;
}
// 按行切割字符串
export function splitLyricsString(data) {
    if (!data) return null;
    // 分割每行
    let arr = data.split("\n"), timeArr = [], lrcArr = [];
    // 分割歌词
    for (let i = 0; i < arr.length; i++) {
        // 分割时间和歌词
        let tempArr = arr[i].split("]");
        if (tempArr.length > 1) {
            // 添加时间和歌词到数组
            timeArr.push(tempArr[0].replace('[', '').slice(0, 5));
            lrcArr.push(tempArr[1]);
        }
    }
    return {
        timeArr,
        lrcArr
    }
}

// 显示歌词
export function displayLyrics(lyricDom, lrcArr) {
    let container = document.createElement('div');
    container.className = 'lyrics-content';
    lrcArr.forEach(txt => {
        let line = document.createElement('p');
        line.textContent = txt;
        container.append(line);
    });
    lyricDom.append(container);
}
// 动态获取keyframes
export function getKeyframes(name) {
    let animation = {}, styleSheets = document.styleSheets;
    for (let i = 0; i < styleSheets.length; i++) {
        let item = styleSheets[i];
        if (item.cssRules[0] && item.cssRules[0].name && item.cssRules[0].name === name) {
            animation.cssRule = item.cssRules[0];
            animation.styleSheet = item;
            animation.index = 0;
        }
    }
    return animation;
}
// 只执行一次函数
export function once(fn) {
    if (Object.prototype.toString.call(fn) !== '[object Function]') throw new TypeError("The parameter must be a function!");
    let count = 0;
    return function () {
        if (count === 0) {
            count++;
            fn.apply(null, arguments);
        }
    }
}